<?php

function product_join(){
	drupal_set_title(t('产品咨询'));
	
	global $base_path;
	$output = '';
	
	$output .= '<div class="classic_top">';
	
	$output .='<div class="product-join-search">';
	
	$output .= '<h3><strong>我们的加盟店铺</strong></h3>';
	
	$output .= '<p>搜索最近接您的专柜，请输入您的地区。如果要获得更详尽咨询，请输入完整地址。';
	
	$html = drupal_get_form('product_search_form');
	
	$output .= drupal_render($html);

	$output .='</div>';
	
	$output .= '<img src="'. base_path() .'sites/default/files/product-search.jpg">';
	$output .= '<div class="clearfix"></div></div>';
	
	$output .='<div id="classic-bottom">';

	$query = db_select('node','n')
			->extend('PagerDefault')
			->limit(10);
	
	if(isset($_GET['name'])) {
		$query->condition('n.title', $_GET['name'], '=');
	}
	$query->fields('n', array('nid'))
    		->condition('n.type', 'jm')
			->orderBy('n.created', 'DESC');
  	$nids = $query->execute()->fetchCol();
	$row = array();
	foreach($nids as $item){
		
		$node = node_load($item);
		$address = field_get_items('node', $node, 'field_jm_address');
		$phone = field_get_items('node', $node, 'field_jm_phone');
		$event = field_get_items('node', $node, 'field_jm_event');
		$row[] =  array('data' => array(
			$node->title,
			$address[0]['value'],
			$phone[0]['value'],
			$event[0]['value']
		));
	}

	$header = array(
		array('data' => 'NAME'),
		array('data' => 'ADDRESS'),
		array('data' => 'PHONE'),
		array('data' => 'EVENT')
	);
	
	$output .= theme('table', array(
		'header' => $header, 
		'rows' => $row, 
		'attributes' => array('class' => array('mytable'))
	));

	$output .=theme('pager');
	
	$output .='</div>';
	
	return $output;
}

function product_search_form() {
	$form = array();
	
	$form['prodcut_search'] = array(
  	'#type' => 'textfield',
		'#size' => 20,
		'#title' => t('查找:')
	);
	
	$form['submit'] = array(
  	'#type' => 'submit',
		'#value' => t('搜索')
	);
	
	return $form;
}

function product_search_form_submit($form,&$form_state){
	$query = array();
   if($form_state['values']['prodcut_search']){
		$query['name'] = $form_state['values']['prodcut_search'];
	}

	$form_state['redirect'] = array('aijia-info/join', array('query' => $query ));
}

function product_job() {

	$output = '';

	$output .= '<div class="classic_top">';
	
	$output .='<img src="'.base_path().'sites/default/files/job1.jpg">';

	$output .='</div>';

	
	$output .='<div id="classic-bottom">';

	$output .='<h3><strong>招聘企业英才</strong></h3>';

	$form = drupal_get_form('aijia_job_form');

	$output .= drupal_render($form);

	
	$output .='</div>';

	return $output;
}

function aijia_job_form() {
	$form = array();
	$form['name'] = array(
  		'#type' => 'textfield',
		'#size' => 20,
		'#title' => t('姓名:'),
		'#required' => true
	);

	$form['address'] = array(
  		'#type' => 'textfield',
		'#size' => 60,
		'#title' => t('地址:')
	);

	$form['age'] = array(
  		'#type' => 'textfield',
		'#size' => 5,
		'#title' => t('年龄:'),
		'#field_suffix' => t('YEARS'),
	);

	$form['edu'] = array(
  		'#type' => 'select',
		'#title' => t('学历:'),
		'#options' => array('大专','本科', '研究生'),
	);

	$form['email'] = array(
  		'#type' => 'textfield',
		'#size' => 20,
		'#title' => t('EMAIL:'),
		'#required' => true
	);

	$form['phone'] = array(
  		'#type' => 'textfield',
		'#size' => 20,
		'#title' => t('联系电话:'),
		'#required' => true
	);

	$form['phone2'] = array(
  		'#type' => 'textfield',
		'#size' => 20,
		'#title' => t('家庭电话:'),
		'#required' => true
	);
	
	$form['submit'] = array(
  	'#type' => 'submit',
		'#value' => t('提交')
	);
	
	return $form;
}

function aijia_job_form_submit($form, &$form_state) {

	$fields = array(
  		'name' => $form_state['values']['name'],
  		'address' => $form_state['values']['address'],
  		'age' => $form_state['values']['age'],
  		'edu' => $form_state['values']['edu'],
  		'email' => $form_state['values']['email'],
  		'phone' => $form_state['values']['phone'],
  		'phone2' => $form_state['values']['phone2']
	);

	db_insert('aijia_job')->fields($fields)->execute();
}


function aijia_job_list() {
	$output = '';

	$str = drupal_get_form('aijia_job_list_form');

	$output .= drupal_render($str);

	return $output;
}

function aijia_job_list_form($form, &$form_state){

	$form = array();

	$query = db_select('aijia_job','a');
			
	$query	->fields('a')
			->orderBy('a.id', 'DESC');
  	$nids = $query->execute()->fetchAll();

  	$header = array(
    	'name' => t('姓名'),
    	'address' => t('地址'),
  	);
	
	$row = array();
	
	foreach($nids as $item){
		$row[$item->id] = array(
    		'name' => $item->name, // 'first_name' was the key used in the header
    		'address' => $item->address, // 'last_Name' was the key used in the header
  		);
	}

	$form['table'] = array(
    	'#type' => 'tableselect',
    	'#header' => $header,
    	'#options' => $row,
    	'#empty' => t('No users found'),
    	'#multiple' => true,
  	);
	
  	$form['submit'] = array( 
		'#type' => 'submit', 
		'#value' => t('删除'),
		'#submit' => array('job_delect')
  	);
	
	return $form;
}

function job_delect($form, &$form_state) {
	$ids = array_filter($form_state['values']['table']);
	foreach($ids as $value)
	{
	  	db_delete('aijia_job')	->condition('id', $value)
  								->execute();
	}
}


function aijia_about(){

	$output = '';

	$output .= '<div class="classic_top">';
	
	$output .='<img src="'.base_path().'sites/default/files/about.jpg">';

	$output .='</div>';

	
	$output .='<div id="classic-bottom">';

	$output .='<img src="'.base_path().'sites/default/files/about2.jpg">';
	$output .='</div>';

	return $output;
}